/*

  The contents of this file are subject to the Mozilla Public License Version
  1.1 (the "License"); you may not use this file except in compliance with
  the License. You may obtain a copy of the License at 
  
           http://www.mozilla.org/MPL/ 
  
  Software distributed under the License is distributed on an "AS IS" basis,
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  for the specific language governing rights and limitations under the License. 
  
  The Original Code is KazeOS Framework.
  
  The Initial Developer of the Original Code is
  ALCARAZ Marc (aka eKameleon) <ekameleon@gmail.com>.
  Portions created by the Initial Developer are Copyright (C) 2004-2010
  the Initial Developer. All Rights Reserved.
  
  Contributor(s) :
  
*/

package evisio.states.chat.display 
{
    import evisio.ApplicationID;
    import evisio.net.StreamOutExpert;

    import lunas.components.buttons.LightButton;

    import system.Strings;

    import vegas.display.Background;
    import vegas.ioc.factory.ObjectFactory;
    import vegas.media.CameraExpert;
    import vegas.media.MicrophoneExpert;

    import flash.events.Event;
    import flash.media.Video;
    import flash.text.TextField;

    /**
     * The user display class.
     */
    public class User extends Background 
    {
        /**
         * Creates a new User instance.
         */
        public function User()
        {
            super();
        }
        
        /**
         * The activate camera button reference.
         */
        public var activateCameraButton:LightButton ;
        
        /**
         * The activate microphone button reference.
         */
        public var activateMicroButton:LightButton ;
        
        /**
         * The camera expert reference of the screen.
         */
        public var cameraExpert:CameraExpert ;
        
        /**
         * The deactivate camera button reference.
         */
        public var deactivateCameraButton:LightButton ;
        
        /**
         * The deactivate microphone button reference.
         */
        public var deactivateMicroButton:LightButton ;
        
        /**
         * The IoC factory reference of this screen.
         */
        public var factory:ObjectFactory ;
        
        /**
         * Determinates the label textfield reference.
         */
        public function get field():TextField 
        {
            return _field ;
        }
        
        /**
         * @private
         */
        public function set field( tf:TextField ):void 
        {
            _field = tf ;
            refreshField() ;
        }
        
        /**
         * Indicates the current label value.
         */
        public function get label():String 
        {
            return _label ;
        }
        
        /**
         * @private
         */
        public function set label( str:String ):void 
        {
            _label = str ;
            refreshField() ;
        }
        
        /**
         * The String pattern to format the label. 
         */
        public var labelPattern:String ;
        
        /**
         * The micro expert reference of the screen.
         */
        public var microExpert:MicrophoneExpert ;
        
        /**
         * Indicates if the user use this camera.
         */
        public function get useCamera():Boolean
        {
            return _useCamera ;
        }
        
        /**
         * Indicates if the user use this micro.
         */
        public function get useMicro():Boolean
        {
            return _useMicro ;
        }
        
        /**
         * The video reference of this screen.
         */
        public var video:Video ;
        
        /**
         * Invoked to activate the Camera.
         */
        public function activateCamera( e:Event = null ):void
        {
            logger.fatal(this + " activateCamera."  ) ;
            refreshCamera( true ) ;
        }    
        
        /**
         * Invoked to activate the Microphone.
         */
        public function activateMicrophone( e:Event = null ):void
        {
            logger.debug(this + " activateMicrophone.") ;
            refreshMicro( true ) ;
        }
        
       /**
         * Invoked to deactivate the Camera.
         */
        public function deactivateCamera( e:Event = null ):void
        {
            logger.debug(this + " deactivateCamera.") ;
            refreshCamera(false) ;
        }
        
        /**
         * Invoked to deactivate the Microphone.
         */
        public function deactivateMicro( e:Event = null ):void
        {
            logger.debug(this + " deactivateMicro.") ;
            refreshMicro( false ) ;
        }
        
        /**
         * Reset the form.
         */
        public function reset( e:Event = null ):void
        {
            if ( field != null )
            {
                field.htmlText = "" ;
            }
        }  
        
        /**
         * Refresh the label field.
         */
        protected function refreshField():void
        {
            if ( _field != null )
            {
               _field.htmlText = Strings.format( labelPattern || "" , _label || "" ) ;
            }
        }
        
        /**
         * Invoked to refresh the camera output.
         */
        protected function refreshCamera( b:Boolean = false ):void
        {
            _useCamera = b  ;
            
            if ( activateCameraButton != null )
            {
                activateCameraButton.visible = !b ;
            }
            
            if ( deactivateCameraButton != null )
            {
                deactivateCameraButton.visible = b ;
            }
            
            if ( factory == null )
            {
                logger.warn(this + " refreshCamera failed with an unknow or null factory reference.") ;   
                return ;
            }
            
            var cameraExpert:CameraExpert = factory.getObject( ApplicationID.CAMERA_EXPERT ) as CameraExpert ;
            if ( cameraExpert == null )
            {
                logger.warn(this + " refreshCamera failed with an unknow or null cameraExpert reference.") ;   
                return ;
            }
            
            if ( video != null )
            {
                if ( _useCamera )
                {    
                    video.attachCamera( cameraExpert.camera ) ;
                }
                else
                {
                    video.attachCamera( null ) ;
                }
                video.visible = _useCamera ;
            }
            else
            {
                logger.warn(this + " activateCamera failed with an unknow or null Video reference.") ;
            }            
                        
            var streamOut:StreamOutExpert = factory.getObject( ApplicationID.STREAM_OUT_EXPERT ) as StreamOutExpert ;
            if ( streamOut != null )
            {
               if ( _useCamera && cameraExpert.camera != null )
               {
                   streamOut.netStream.attachCamera( cameraExpert.camera ) ;
               }
               else
               {
                      streamOut.netStream.attachCamera( null ) ;
               }
            }
        }
        
        /**
         * Invoked to refresh the micro output.
         */
        protected function refreshMicro( b:Boolean = false ):void
        {
            _useMicro = b  ;
            
            if ( activateMicroButton != null )
            {
                activateMicroButton.visible = !b ;
            }
            if ( deactivateMicroButton != null )
            {
                deactivateMicroButton.visible = b ;
            } 
            
            if ( factory == null )
            {
                logger.warn(this + " refreshMicro failed with an unknow or null factory reference.") ;   
                return ;
            }
            
            var microExpert:MicrophoneExpert = factory.getObject( ApplicationID.MICRO_EXPERT) as MicrophoneExpert ;
            
            if ( microExpert == null )
            {
                logger.warn(this + " refreshMicro failed with an unknow or null microphone expert reference.") ; 
                return ;
            }
            
            var streamOut:StreamOutExpert = factory.getObject( ApplicationID.STREAM_OUT_EXPERT ) as StreamOutExpert ;
            if ( streamOut != null )
            {
                if ( useMicro && microExpert.microphone != null )
                {
                    streamOut.netStream.attachAudio( microExpert.microphone ) ;
                }
                else
                {
                    streamOut.netStream.attachAudio( null ) ;
                }
            }
        }
        
        /**
         * Invoked when the display is removed from the stage.
         */
        protected override function removedFromStage( e:Event = null ):void
        {
            refreshCamera( false ) ;
            refreshMicro( false )  ;
        }
        
        /**
         * @private
         */
        private var _field:TextField ;
        
        /**
         * @private
         */
        private var _label:String ;
        
        /**
         * @private
         */
        private var _useCamera:Boolean ;
        
        /**
         * @private
         */
        private var _useMicro:Boolean ;
    }
}
